import rclpy
from rclpy.node import Node
from rclpy.qos import DurabilityPolicy
from rclpy.qos import HistoryPolicy
from rclpy.qos import QoSProfile
from rclpy.qos import ReliabilityPolicy
from rclpy.qos_event import SubscriptionEventCallbacks
from std_msgs.msg import String

class MinimalSubscriber(Node):

    def __init__(self):
        qos = QoSProfile(depth=10)
        #设置不同的qos策略
        qos.history = HistoryPolicy.RMW_QOS_POLICY_HISTORY_KEEP_ALL
        custom_callback = lambda event: print("Custom Incompatible Callback!")
        callbacks = SubscriptionEventCallbacks()
        callbacks.incompatible_qos = custom_callback
        super().__init__('minimal_subscriber')
        self.subscription = self.create_subscription(
            String,                                              # CHANGE
            'topic',
            self.listener_callback,
            qos,
            event_callbacks=callbacks)

    def listener_callback(self, msg):
            self.get_logger().info('I heard: %s' % msg.data) # CHANGE


def main(args=None):
    rclpy.init(args=args)

    minimal_subscriber = MinimalSubscriber()

    rclpy.spin(minimal_subscriber)
    minimal_subscriber.destroy_node()
    rclpy.shutdown()


if __name__ == '__main__':
    main()
